网站自动发布每天60秒读懂世界文章源码更新

87次阅读
没有评论

共计 3875 个字符,预计需要花费 10 分钟才能阅读完成。

[c-alert type="error"]API接口已更新,请移步网站自动发布每天60秒读懂世界文章源码2025最新版[/c-alert]

此前在网站上搜了很多wordpress每天自动发布60s读懂世界文章的教程,但是全部都是引用知乎上的一位作者的文章(目前该作者已停更,已经迁移到公众号了),无意间发现了alapi这个网站,有关于60s新闻早报的api接口,经过了一番瞎折腾,终于搞出来了代码,经过博主的测试,api基本是稳定的,可以拿来一用,可以根据以下说明来配置部署:

获取token

利用alapi网站中的早报api,实现每天早8点发布wordpress文章,需要在alapi中注册并获得token,alapi地址为:https://www.alapi.cn

网站自动发布每天60秒读懂世界文章源码更新

 

PHP代码

以下为php代码,在网站根目录下新建php文件,并将以下代码拷贝到文件中,需修改代码行的token

1、文章分类默认为“60S看世界”

2、文章的标题为“yyyy年mm月dd日,星期X,60s读懂全世界!”

3、为了方便百度收录遍历,文章的postname设置为“yyyymmdd-60s-news”

4、文章已经有了头图,博主的wp主题可在分类页面直接识别头图为缩略图,但是为了照顾有些主题只识别特色图像,博主在代码中添加了代码,将头图自动识别为特色图像,建议保留此项配置

 

[loginshow]

//由无能小子编写,二开请保留作者名称
//作者:无能小子
//无能小子博客站:https://www.wnboy.com
<?php
require_once(dirname(__FILE__) . '/wp-load.php');
$api_url = "https://v2.alapi.cn/api/zaobao";
$api_token = 'xxxxxxxxxx'; // 请确保替换为您的实际API Token
$api_url_with_token = add_query_arg('token', $api_token, $api_url);
$response = wp_remote_get($api_url_with_token);
if (is_wp_error($response)) {
    wp_die('API调用失败:' . $response->get_error_message());
}
$json_data = wp_remote_retrieve_body($response);
$data = json_decode($json_data, true);
if (isset($data['code']) && $data['code'] == 200 && $data['msg'] == 'success' && isset($data['data']['news'])) {
    $news_list = $data['data']['news'];
    $head_image_url = $data['data']['head_image'] ?? ''; 
    $combined_content = ''; 
    if (!empty($head_image_url)) {
        $image_data = wp_remote_get($head_image_url);
        if (is_wp_error($image_data)) {
            wp_die('无法下载图像:' . $image_data->get_error_message());
        }
        $image_content = wp_remote_retrieve_body($image_data);
        $upload_dir = wp_upload_dir();
        $image_filename = basename($head_image_url); 
        $image_filepath = $upload_dir['path'] . '/' . $image_filename;
        file_put_contents($image_filepath, $image_content);

        $attachment = [
            'post_mime_type' => 'image/' . pathinfo($image_filename, PATHINFO_EXTENSION), // 根据文件扩展名设置MIME类型
            'post_title'     => basename($image_filename),
            'post_content'   => '',
            'post_status'    => 'inherit'
        ];
        $attach_id = wp_insert_attachment($attachment, $image_filepath);
        require_once(ABSPATH . 'wp-admin/includes/image.php');
        $attach_data = wp_generate_attachment_metadata($attach_id, $image_filepath);
        wp_update_attachment_metadata($attach_id, $attach_data);
        $image_html = wp_get_attachment_image($attach_id, 'full'); 
        $combined_content = $image_html . '<p></p>' . $combined_content; 
    }

    foreach ($news_list as $news_item) {
        $cleaned_content = trim(preg_replace('/^\d+。\s*/', '', $news_item)); // 根据实际情况调整字段名
        $combined_content .= '<p>' . wp_kses_post($cleaned_content) . '</p>';
    }

    $current_date = new DateTime();
    $formatted_date = $current_date->format('Y年m月d日');
    $day_of_week_en = $current_date->format('l');
    $slug_date = $current_date->format('Ymd');
    $day_of_week_map = [
        'Monday'    => '星期一',
        'Tuesday'   => '星期二',
        'Wednesday' => '星期三',
        'Thursday'  => '星期四',
        'Friday'    => '星期五',
        'Saturday'  => '星期六',
        'Sunday'    => '星期日'
    ];
    $day_of_week_cn = $day_of_week_map[$day_of_week_en] ?? '未知的星期';
    $post_data = [
        'post_title'    => "{$formatted_date} ,$day_of_week_cn ,60s读懂全世界!",
        'post_name'   => "{$slug_date}-60s-news",
        'post_content'  => $combined_content,
        'post_status'   => 'publish',
        'post_type'     => 'post',
        'post_author'   => 1,
        'post_date'     => current_time('mysql'),
    ];
    $post_id = wp_insert_post($post_data);
    if (is_wp_error($post_id)) {
        wp_die('文章发布失败:' . $post_id->get_error_message());
    } else {
        $category_names = ['活动线报', '60s看世界']; // 替换为实际的分类名称
        $category_ids = [];
        foreach ($category_names as $category_name) {
            $term = term_exists($category_name, 'category');
            if ($term === null || $term['term_id'] == 0) {
                $new_term = wp_insert_term($category_name, 'category');
                if (!is_wp_error($new_term)) {
                    $category_ids[] = $new_term['term_id'];
                } else {
                    error_log('创建分类失败:' . $new_term->get_error_message());
                }
            } else {
                $category_ids[] = $term['term_id'];
            }
        }

        wp_set_post_categories($post_id, $category_ids);
        set_post_thumbnail($post_id, $attach_id);
        echo '文章已成功发布,ID为:' . $post_id;
    }
} else {
    wp_die('API响应错误:' . ($data['msg'] ?? '未知错误'));
}
?>

[/loginshow]

 

自动化执行

在宝塔面板生成一个URL的自动化执行脚本,url填写https://你的域名/文件名.php,时间为每天7点执行(alapi中描述的是每天1点30就能看到第二天的新闻内容了,但是我感觉7点靠谱点),因为我linux运维管理工具使用的是1panel,这里就不截图了。

新闻截图

总体下来还可以,12月22日-星期日那天脚本没有获取到新闻,中午重新执行了下,成功了,后台看alapi日志应该是没有及时更新新闻内容。

整篇新闻给人的感觉还是比较简约的,可以拿来用一用~

网站自动发布每天60秒读懂世界文章源码更新

正文完
 0
无能小子
版权声明:本站原创文章,由 无能小子 于2024-12-24发表,共计3875字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
验证码